package com.klikmee.sms.core.base.communicator.starion;

public class BulkResponseParser {
	/**
	 * Enumeration of possible bulk message statuses 
	 * @author makcro
	 *
	 */
	public enum ResponseStatus{
		
		SUCCESS("success"),
		ERR_ACCESS_DENIED("err_access_denied"),
		ERROR_SEQ_MISSING("err_sequence_missing");		;
		
		private ResponseStatus(String value) {
			this.value = value;
		}
		private String value;
		
		public String getValue() {
			return value;
		}
	}
	
	/**
	 * Parses HTTP response in form of 
	 * success
	 * broj:id_poruke
	 * ....
	 * broj:id_poruke
	 * and creates an instance of {@link BulkStatus} obj.		
	 * @param httpResponse response returned from http method after message was sent
	 * @return object containing status report info
	 */
	public static BulkStatus parse(String httpResponse){
		BulkStatus status = new BulkStatus();
		
		// split response on new line and check if sending status is ok
		String[] responseLines = httpResponse.split( "\\r?\\n" );
		if(responseLines[0].equals( ResponseStatus.SUCCESS.getValue() )){
			// fill status with success data by parsing next line of data
			String[] singleReceiverInfo = responseLines[1].split( ":" );
			
			status.setReceiver( singleReceiverInfo[0] );
			status.setStartionMessageId( singleReceiverInfo[1] );
			status.setStatus( ResponseStatus.SUCCESS.getValue() );
		}
		// parse failure info
		else {
			status.setStatus( responseLines[0] );
			status.setStartionMessageId( responseLines[0] );
		}
		
		return status;
	}
	
	
}
